package qy.jalgotrade.utils.resamplebase;

import java.time.Instant;
import java.time.ZonedDateTime;

import qy.jalgotrade.bar.Bar;

/**
 * @author qy
 *
 */
public class IntraDayRange extends TimeRange {

	private final ZonedDateTime __begin;

	private final ZonedDateTime __end;

	/**
	 * 
	 * @param dateTime dateTime
	 * @param frequency frequency
	 */
	public IntraDayRange(ZonedDateTime dateTime, Bar.Frequency frequency) {

		super();

		assert frequency.getValue() < Bar.Frequency.DAY.getValue();

		long ts = dateTime.toEpochSecond();
		long slot = ts / (long) frequency.getValue();
		long slotTs = slot * frequency.getValue();
		__begin = ZonedDateTime.ofInstant(Instant.ofEpochSecond(slotTs), dateTime.getZone());
		__end = __begin.plusSeconds(frequency.getValue());
	}

	/*
	 * (non-Javadoc)
	 * @see qy.jalgotrade.utils.resamplebase.TimeRange#belongs(java.time.ZonedDateTime)
	 */
	@Override
	public boolean belongs(ZonedDateTime dateTime) {

		return (dateTime.isEqual(__begin) || dateTime.isAfter(__begin)) && dateTime.isBefore(__end);
	}

	/*
	 * (non-Javadoc)
	 * @see qy.jalgotrade.utils.resamplebase.TimeRange#getBeginning()
	 */
	@Override
	public ZonedDateTime getBeginning() {

		return __begin;
	}

	/*
	 * (non-Javadoc)
	 * @see qy.jalgotrade.utils.resamplebase.TimeRange#getEnding()
	 */
	@Override
	public ZonedDateTime getEnding() {

		return __end;
	}
}
